<?php

namespace Admin\Controller;


use Admin\Common\Page;

class LogSalesController extends BaseController{

    public function index(){



        $map['a.type'] = 0;
        $order_id = I('get.order_id');
        if (!empty($order_id)) {
            $map['a.order_id'] = $order_id;
        }

        $interval_time = I('get.interval_time');
        if (!empty($interval_time)) {
            $time=explode('~',$interval_time);
            $time[0]=$time[0].' 00:00:00';
            $time[1]=$time[1].' 23:59:59';
            $map['a.create_time'] = [
                ['gt',$time[0]],
                ['lt',$time[1]]
            ];
        }

        $state_order = I("get.state_order");
        if($state_order == "-1" || $state_order === ""){
            $_GET['state_order'] = -1;
        }else{
            $map['a.state_order'] = $state_order;
        }


        $is_online = I("get.is_online");
        if($is_online == "-1" || $is_online === ""){
            $_GET['is_online'] = -1;
        }else{
            $map['a.is_online'] = $is_online;
        }




        $count = D('OrderBook')->alias('a')->join('card on a.user_id=card.user_id','left')->where($map)->count();// 查询满足要求的总记录数
        $Page = new  \Admin\Common\Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
//        $Page = new  \Admin\Common\Page($count , 1);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $this->assign('page' , $show);// 赋值分页输出
        $data = D('OrderBook')->alias('a')->relation(TRUE)
            ->where($map)
            ->join('card b on a.user_id=b.user_id','left')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->field('a.*,b.card_id')
            ->order('a.create_time desc')
            ->select();

        foreach ($data as $key => $val) {
            $data[ $key ]['address'] = json_decode($val['address'] , TRUE);
        }


        if(I("get.execl")==1){
            $data = D('OrderBook')->alias('a')
                ->join('left join card b on a.user_id=b.user_id')
                ->where($map)
                ->field('b.card_id,a.*')
                ->order('a.create_time desc')
                ->select();
            $file = excel_export($data);
            ouput_Browser($file);
        }else{
            $this->data = $data;
            $this->display();
        }
    }

    
}